package demoMod.stsconsole.helpers;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.core.AbstractCreature;
import demoMod.stsconsole.interfaces.DamageSubscriber;

import java.lang.reflect.InvocationTargetException;

public class DamageInfo {
    public int base;
    public AbstractCreature source;
    public float output;
    public DamageType type;

    public DamageInfo(int base, AbstractCreature source) {
        this(base, source, DamageType.NORMAL);
    }

    public DamageInfo(int base, AbstractCreature source, DamageType type) {
        this.base = base;
        this.output = this.base;
        this.source = source;
        this.type = type;
    }

    public void applyPowers(AbstractCreature target) {
        //先结算攻击方
        this.output = this.base;
        ConsoleEntry.triggerSubscribers(
            DamageSubscriber.class,
            "onAttackToChangeDamage",
            new Class[] { float.class, DamageInfo.class, AbstractCreature.class },
            new Object[] { this.output, this, target },
            (method, params, subscriber) -> {
                try {
                    params[0] = method.invoke(subscriber, params);
                    this.output = (float) params[0];
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        );

        //后结算防守方
        ConsoleEntry.triggerSubscribers(
            DamageSubscriber.class,
            "onAttackedToChangeDamage",
            new Class[] { float.class, DamageInfo.class, AbstractCreature.class },
            new Object[] { this.output, this, target },
            (method, params, subscriber) -> {
                try {
                    params[0] = method.invoke(subscriber, params);
                    this.output = (float) params[0];
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        );
    }

    public enum DamageType {
        NORMAL, HP_LOSS, THORNS
    }
}
